package app.netant.book.feisuxs;

import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import app.netant.book.base.BaseDetail;
import app.netant.book.base.BaseEntity;
import app.netant.book.base.BasePage;
import cn.lily.com.enviroment.Enviroment;

public class FeisuxsPage extends BasePage {
	
	private String type = "";

	public FeisuxsPage(String netSite) {
		super(netSite);
		this.tableKey = "feisuxs";
		this.selectKey = ".main #centerm #content table.grid tbody tr td.odd a";
		this.preHref = "http://www.feisuxs.net";
		this.analyseType();
	}

	@Override
	public String buildHref(Element element) {
		String href = element.attr("href");
		return href;
	}

	@Override
	public BaseEntity buildEntity() {
		return new FeisuxsEntity();
	}

	@Override
	public BaseDetail buildDetail(String href) {
		return new FeisuxsDetail(href);
	}
	
	public void analyseType(){
		Elements element = this.select(".main #centerm #content table.grid caption");
		if(element.size()>0){
			String type = element.text();
			int idx = type.indexOf("-");
			if(idx>0){
				type = type.substring(idx+1);
			}		
			this.type = type.trim();
		}
	}

	@Override
	public void afterEntity(BaseEntity entity, Element element) {
		
		Element tr = element.parent().parent();
		Elements tds = tr.select("td");
		
		if(tds.size()>5){
			String writer = tds.get(2).text();
			String date = tds.get(4).text();
			String state = tds.get(5).text();
			
			entity.setNovelWriter(writer);
			entity.setNovelDate(date);
			entity.setNovelState(state);
			
		}
		
		if(this.type != null && !"".equals(this.type)){
			entity.setNovelType(this.type);
		}
		
	}

	
	public static void main(String[] args) {
		Enviroment.createEnv();
		Enviroment.getEnv().getLogBean().beginLog();
		FeisuxsPage page = new FeisuxsPage("http://www.feisuxs.net/fenlei2/fenlei2-2.html");
		page.analyseHref();
	}
	
}
